//
//  MyStoreViewController.m
//  iStore
//
//  Created by Geek on 15/4/23.
//  Copyright (c) 2015年 mirrtalk. All rights reserved.
//

#import "MyStoreViewController.h"
#import "MyUtil.h"
#import "StoreManagerViewController.h"
#import "ApplyViewController.h"
#import "PublicityViewController.h"
#import "AccountManagementViewController.h"
#import "SendADViewController.h"
#import "NewsViewController.h"
#import "SetViewController.h"
#import "PublicityManageViewController.h"
#import "RequestEngine.h"


@interface MyStoreViewController ()
{
    UIImageView *_imageView;
    BOOL BTNCLICK;
    UILabel *msgLabel;
}
@end

@implementation MyStoreViewController


- (void)viewDidLoad {
    [super viewDidLoad];
   self.title = @"声财有道";
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self createScrollView];
    [self createButton];
//    [self createImageView];
    [self createImageBtn];
    [self createfootViewButton];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationItem.leftBarButtonItem = nil;
    NSString *userID = [[NSUserDefaults standardUserDefaults]objectForKey:kUserId];
    [RequestEngine RequestEngineWithDic:@{@"userID":userID} URL:@"message/getAlive" completed:^(NSString *errorcode, NSDictionary *resultDict) {
        if ([errorcode isEqualToString:@"0"]) {
            [self createMessageNumber:resultDict];
        }
    }];
}
- (void)createMessageNumber:(NSDictionary *)dict
{
    if (![[NSString stringWithFormat:@"%@",dict[@"count"]] isEqualToString:@"0"]) {
        if (!msgLabel) {
            msgLabel = [[UILabel alloc]initWithFrame:CGRectMake(22, self.view.bounds.size.height - 55, 14, 14)];

            msgLabel.layer.masksToBounds = YES;
            msgLabel.layer.cornerRadius = msgLabel.frame.size.height/2;
            msgLabel.backgroundColor = [UIColor redColor];
            msgLabel.textColor = [UIColor whiteColor];
            msgLabel.textAlignment = NSTextAlignmentCenter;
            msgLabel.font = [UIFont systemFontOfSize:10];
            [self.view addSubview:msgLabel];
        }
        msgLabel.text = [NSString stringWithFormat:@"%@",dict[@"count"]];
        
    }
    else
        [msgLabel removeFromSuperview];
}

#pragma mark -- 创建滚动视图---
- (void)createScrollView
{
//    UIScrollView *sv = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0,self.view.bounds.size.width, self.view.bounds.size.height/4)];
//   
    UIImageView *imageView = [[UIImageView alloc]init];
    imageView.frame = CGRectMake(0, 64,ScreenWidth, ScreenHeight/4);
    imageView.image = [UIImage imageNamed:@"没白边.jpg"];
    [self.view addSubview: imageView];
    
//    [self.view addSubview:sv];
}

#pragma mark -- 创建classa按钮---
- (void)createButton
{
    
    CGFloat BtnW = (self.view.bounds.size.width - 16)/2;
    CGFloat BtnH = self.view.bounds.size.height/11;
    CGFloat BtnX1 = 5;
    CGFloat BtnX2 = 11 + BtnW;
    CGFloat BtnY1 = self.view.frame.size.height/4 + 69;
    CGFloat BtnY2 = BtnY1 + 5 + BtnH;

    UIButton * storeBtn = [MyUtil createBtnFrame:CGRectMake(BtnX1,BtnY1,BtnW,BtnH) image:nil selectedImage:nil target:self action:@selector(btnClick:)tag:100];
    storeBtn.backgroundColor = [UIColor whiteColor];
    
    UIButton * publicityBtn = [MyUtil createBtnFrame:CGRectMake(BtnX2,BtnY1, BtnW,BtnH) image:nil selectedImage:nil target:self action:@selector(btnClick:) tag:101];
    publicityBtn.backgroundColor = [UIColor whiteColor];
    
    UIButton * channelBtn = [MyUtil createBtnFrame:CGRectMake(BtnX1,BtnY2, BtnW, BtnH) image:nil selectedImage:nil target:self action:@selector(btnClick:)tag:102];
    channelBtn.backgroundColor = [UIColor whiteColor];
    
    
    UIButton * accountBtn = [MyUtil createBtnFrame:CGRectMake(BtnX2, BtnY2, BtnW, BtnH) image:nil selectedImage:nil target:self action:@selector(btnClick:) tag:103];
    accountBtn.backgroundColor = [UIColor whiteColor];
    
    NSArray * imageArray  = @[@"storeManager",@"xuanchuan",@"pindao",@"zhanghu"];
    NSArray * labelArray  = @[@"店铺管理",@"宣传效果",@"专属频道",@"我的账户"];
    
    for (int i = 0; i< imageArray.count; i ++ ) {
        UIImageView *image = [MyUtil createImageViewFrame:CGRectMake(10,0,BtnH,BtnH) imageName:imageArray[i]];
        image.tag = i;
        UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(expandBtnClick:)];
        [image addGestureRecognizer:gesture];
        
        UILabel *label = [MyUtil createLabelFrame:CGRectMake(15+ BtnH, 0, BtnH *2, BtnH) title:labelArray[i] textAlignment:NSTextAlignmentLeft];
        if (i == 0) {
            [storeBtn addSubview:image];
            [storeBtn addSubview:label];
        }if (i == 1) {
            [publicityBtn addSubview:image];
            [publicityBtn addSubview:label];
        }if (i == 2) {
            [channelBtn addSubview:image];
            [channelBtn addSubview:label];
        }if ( i == 3) {
            [accountBtn addSubview:image];
            [accountBtn addSubview:label];
        }
    }
    [self.view addSubview:channelBtn];
    [self.view addSubview:accountBtn];
    [self.view addSubview:publicityBtn];
    [self.view addSubview:storeBtn];
    
}
- (void)expandBtnClick:(UITapGestureRecognizer *)gesture
{
    NSInteger tag = gesture.view.tag;
    [self btnClick:(UIButton *)[self.view viewWithTag:tag]];
}
- (void)btnClick: (UIButton *)btn
{
    if (btn.tag == 100 || btn.tag == 0) {
        StoreManagerViewController * sVC = [[StoreManagerViewController alloc]init];
        [self.navigationController pushViewController:sVC animated:YES];
    }if (btn.tag == 102 || btn.tag == 2) {
//        ApplyViewController * aVC = [[ApplyViewController alloc]init];
//        [self.navigationController pushViewController:aVC animated:YES];
        alert(@"敬请期待");
        
    }if ( btn.tag == 101 || btn.tag == 1) {
        PublicityViewController *pVC = [[PublicityViewController alloc]init];
        
        [self.navigationController pushViewController:pVC animated:YES];
       
    }if (btn.tag == 103 || btn.tag == 3) {
        AccountManagementViewController * aMVC = [[AccountManagementViewController alloc]init];
        [self.navigationController pushViewController:aMVC animated:YES];
    }
}

//#pragma mark --- 创建汽车图片---
//- (void)createImageView
//{
//    NSArray * imageArr = @[@"car01",@"car02",@"car03"];
//
//    CGFloat imageW = (self.view.bounds.size.width - 15)/3;
//    CGFloat imageH = self.view.bounds.size.height/7;
//    CGFloat imageX = 5;
//    CGFloat imageY = (self.view.bounds.size.height * 6)/11+ 8;
//    
//    for (int i = 0 ; i < imageArr.count ; i ++) {
//        UIImageView *carImage = [MyUtil createImageViewFrame:CGRectMake((imageX + imageW) * i , imageY, imageW, imageH) imageName:imageArr[i]];
//        [self.view addSubview:carImage];
//    }
//
//}

#pragma  mark -- 创建开始宣传按钮---
- (void)createImageBtn
{
    _imageView = [MyUtil createImageViewFrame:CGRectMake(-7, (self.view.bounds.size.height *11)/18, self.view.bounds.size.width, self.view.frame.size.height/1.8) imageName:@"喊"];
    _imageView.userInteractionEnabled = YES;
    [self.view addSubview:_imageView];
    
    UIButton * btn = [MyUtil createBtnFrame:CGRectMake(35,self.view.bounds.size.height/1.4,self.view.bounds.size.width/1.2,self.view.frame.size.height/5) image:nil selectedImage:nil target:self action:@selector(imageBtnClick:) tag:100];
        BTNCLICK = YES;

    [self.view addSubview:btn];
    
}
- (void)imageBtnClick:(UIButton *)btn
{
//    NSLog(@"222");
    [_imageView setImage:[UIImage imageNamed:@"喊2.png"]];
    
    [self performSelector:@selector(btnImageClick:) withObject:nil afterDelay:0.2];
    
    PublicityManageViewController *SVC= [[PublicityManageViewController alloc]init];
    
    [self.navigationController pushViewController:SVC animated:YES];
}

- (void)btnImageClick:(UIButton *)btn
{
    [_imageView setImage:[UIImage imageNamed:@"喊"]];
}

#pragma  mark -- 底部的按钮 --
- (void)createfootViewButton
{
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0,self.view.bounds.size.height - 50,self.view.bounds.size.width ,50)];
    imageView.image = [UIImage imageNamed:@"backColor"];
    imageView.userInteractionEnabled = YES;
    [self.view addSubview:imageView];
    
    UIButton * newsBtn = [MyUtil createBtnFrame:CGRectMake(5, 0, 27, 27) image:@"xiaoxi" selectedImage:nil target:self action:@selector(viewBtnClick:) tag:100];
    [imageView addSubview:newsBtn];
    
//    UILabel * newLabel = [MyUtil createLabelFrame:CGRectMake(2,30, 40, 17) title:@"消息" textAlignment:NSTextAlignmentCenter];
    UIButton *newLabel = [[UIButton alloc]initWithFrame:CGRectMake(2,30, 40, 17)];
    [newLabel setTitle:@"消息" forState:UIControlStateNormal];
    newLabel.titleLabel.font = [UIFont systemFontOfSize:12];
    newLabel.titleLabel.textColor = [UIColor whiteColor];
    newLabel.tag = 103;
    [newLabel addTarget:self action:@selector(viewBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [imageView addSubview:newLabel];

    
    UIButton * setBtn = [MyUtil createBtnFrame:CGRectMake(ScreenWidth - 35,2, 27, 27) image:@"shezhi" selectedImage:nil target:self action:@selector(viewBtnClick:) tag:101];
    [imageView addSubview:setBtn];
    
 //   UILabel *setLabel = [MyUtil createLabelFrame:CGRectMake(self.view.bounds.size.width- 45, 32, 40, 17) title:@"设置" textAlignment:NSTextAlignmentCenter];
    UIButton *setLabel = [[UIButton alloc]initWithFrame:CGRectMake(self.view.bounds.size.width- 40, 32, 40, 17)];
    [setLabel setTitle:@"设置" forState:UIControlStateNormal];
    setLabel.titleLabel.font = [UIFont systemFontOfSize:12];
    setLabel.titleLabel.textColor = [UIColor whiteColor];
    setLabel.tag = 104;
    [setLabel addTarget:self action:@selector(viewBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [imageView addSubview:setLabel];
    
}

- (void)viewBtnClick:(UIButton *)btn
{
    if (btn.tag == 100 || btn.tag == 103) {
        NewsViewController * nVC = [[NewsViewController alloc]init];
        [self.navigationController pushViewController:nVC animated:YES];
        
    }if (btn.tag == 101 || btn.tag == 104) {
        SetViewController * sVC = [[SetViewController alloc]init];
        [self.navigationController pushViewController:sVC animated:YES];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
